const express = require('express');
const router = express.Router();
const { pool } = require('../config/database');

// Admin login
router.post('/login', async (req, res) => {
  try {
    const { username, password } = req.body;
    
    if (!username || !password) {
      return res.status(400).json({
        success: false,
        message: 'Username and password are required'
      });
    }

    // Simple admin verification (production needs more secure verification)
    if (username === 'admin' && password === 'admin123') {
      res.json({
        success: true,
        message: 'Admin login successful',
        data: {
          token: 'admin_token_' + Date.now(),
          user: { username: 'admin', role: 'admin' }
        }
      });
    } else {
      res.status(401).json({
        success: false,
        message: 'Invalid username or password'
      });
    }
  } catch (error) {
    console.error('Admin login error:', error);
    res.status(500).json({
      success: false,
      message: 'Admin login failed'
    });
  }
});

// Get statistics
router.get('/stats', async (req, res) => {
  try {
    res.json({
      success: true,
      data: {
        totalUsers: 1250,
        totalOrders: 3420,
        totalRevenue: 125600.50,
        todayOrders: 45
      }
    });
  } catch (error) {
    console.error('Get admin stats error:', error);
    res.status(500).json({
      success: false,
      message: 'Failed to get statistics'
    });
  }
});

module.exports = router;